package cn.hahah.module.blog.controller.admin.userhistorysearch;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userhistorysearch.vo.*;
import cn.hahah.module.blog.dal.dataobject.userhistorysearch.UserHistorySearchDO;
import cn.hahah.module.blog.service.userhistorysearch.UserHistorySearchService;

@Tag(name = "管理后台 - 用户搜索记录")
@RestController
@RequestMapping("/blog/user-history-search")
@Validated
public class UserHistorySearchController {

    @Resource
    private UserHistorySearchService userHistorySearchService;
    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户搜索记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:create')")
    public CommonResult<Long> createUserHistorySearch(@Valid @RequestBody UserHistorySearchSaveReqVO createReqVO) {
        return success(userHistorySearchService.createUserHistorySearch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户搜索记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:update')")
    public CommonResult<Boolean> updateUserHistorySearch(@Valid @RequestBody UserHistorySearchSaveReqVO updateReqVO) {
        userHistorySearchService.updateUserHistorySearch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户搜索记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:delete')")
    public CommonResult<Boolean> deleteUserHistorySearch(@RequestParam("id") Long id) {
        userHistorySearchService.deleteUserHistorySearch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户搜索记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:query')")
    public CommonResult<UserHistorySearchRespVO> getUserHistorySearch(@RequestParam("id") Long id) {
        UserHistorySearchDO userHistorySearch = userHistorySearchService.getUserHistorySearch(id);
        return success(BeanUtils.toBean(userHistorySearch, UserHistorySearchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户搜索记录分页")
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:query')")
    public CommonResult<PageResult<UserHistorySearchRespVO>> getUserHistorySearchPage(@Valid UserHistorySearchPageReqVO pageReqVO) {
        PageResult<UserHistorySearchDO> pageResult = userHistorySearchService.getUserHistorySearchPage(pageReqVO);
        PageResult<UserHistorySearchRespVO> result = BeanUtils.toBean(pageResult, UserHistorySearchRespVO.class);
        for (UserHistorySearchRespVO userHistorySearchRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userHistorySearchRespVO.getUserId());

            if( usersDO != null){
                userHistorySearchRespVO.setUserName(usersDO.getUsername());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户搜索记录 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-history-search:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserHistorySearchExcel(@Valid UserHistorySearchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserHistorySearchDO> list = userHistorySearchService.getUserHistorySearchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户搜索记录.xls", "数据", UserHistorySearchRespVO.class,
                        BeanUtils.toBean(list, UserHistorySearchRespVO.class));
    }

}